// pages/signIn/signIn.js
const http = require('../../utils/http.js');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    percent: 0,
    status: 'normal',
    signModal: true,
    dateArr: [
      // { a: '1日', b: 1 },
      // { a: '2日', b: 1 },
      // { a: '3日', b: 0 },
      // { a: '4日', b: 1, c: 1 },
      // { a: '5日', b: 0 }

    ],
    uid:'',
    qianTips:false,
     hasQian:0
  },

  //签到
  signIn(e) {
    var uid = app.globalData.uid;
    var formId = e.detail.formId;
    wx.request({
      url: app.globalData.host + 'procedure_t',
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: { p: 'R2013001|' + formId + '|' + uid }
    })

    http.fetchApi('procedure_t', { p: 'R2013005|' + uid }).then(res => {
      if (res.data.code == 1) {
        var signReward = res.data.results[0];
       
        if (signReward.bl_type==0){
          $Toast({
            content: '您今天已经签过到了',
            type: 'warning'
          });
          return
        }
        
        this.querySign(this.data.today);
        this.queryIsQian()
        this.setData({
          signModal: false
        })

      
        this.setData({
          signReward
        })
      }
    })
  },


  ///我收下了
  handleAccept() {
    this.setData({
      signModal: true
    })

    if (this.data.signReward.id!=0){
      wx.navigateTo({
        url: "/pages/wishList/wishList?wishId=" + this.data.signReward.id
      })
    }
    
  },

  //点击礼物发布心愿单
  towishCard(e){
    console.log(e.currentTarget.dataset.wid)
    var wid = e.currentTarget.dataset.wid;
    if(wid==0){
      $Toast({
        content: '请签到后领取心愿单',
        type: 'warning'
      });
      return
    }
    wx.navigateTo({
      url: "/pages/wishList/wishList?wishId=" + wid
    })
  },

  //去补签页面
  toBuqian() {
    this.toSignDetail();
  },
  //跳转签到详情
  toSignDetail() {
    wx.navigateTo({
      url: '../signInDetail/signInDetail',
    })
  },

//邀请好友助力
  yaoZhuli(e){
    var zl_id = e.currentTarget.dataset.zlid;
    wx.navigateTo({
      url: '../ShareInvitation/ShareInvitation?zl_id=' + zl_id
    })
  },

  //帮别人助力
  helpZhuli(e) {
    var zl_id = e.currentTarget.dataset.zlid;
    wx.navigateTo({
      url: '../ShareInvitation/ShareInvitation?zl_id=' + zl_id +'&shareid=1'
    })
  },
  //去好友助力列表页面
  toAssistance() {
    wx.navigateTo({
      url: '../assistanceList/assistanceList',
    })
  },

  //签到查询
  querySign(date) {
    var uid = app.globalData.uid;
    // http.fetchApi('procedure_t', { p: 'R2013004|' + uid + '|' + date }).then(res => {
    //   if (res.data.code == 1) {
    //     var signArr = res.data.results;


    //     var dateArr = [];
       
    //     var today = new Date().getDate();
    //     var flor = Math.ceil((today / 5));
    //     var starDate = 5 * flor - 4;
    //     if (flor == 7) {
    //     starDate = 27
    //     }
    //     for (var i = starDate; i < starDate + 5; i++) {

    //       dateArr.push({ a: `${i}日`, b: 0 })
    //       if (i >= today){
    //         dateArr[i - starDate].b = 2
    //       }
    //       signArr.forEach((item, index) => {
            
    //         var a = item.sign_date.split(' ')[0].split('-')[2] / 1
    //         if(a==i){
    //           dateArr[i - starDate].b=1
    //         }
            
            
    //       })

          
    //     }
     
    //     var todayIdx = dateArr.findIndex(item => item.a == today+'日');
    //    // console.log(todayIdx)
    //     this.setData({
    //       signArr,
    //       dateArr,
    //       todayIdx
    //     })

    //   }
    // })
    var todayDate = new Date().getDate();
    
    console.log(new Date().getDate())
    http.fetchApi('p_dates_lw', { datas: date, uid }).then(res=>{
      if(res.data.code==1){
        var dateArr = res.data.data;
        var todayIdx = dateArr.findIndex(item => item.id == todayDate);
        var quanArr = dateArr.filter(item => item.lws > 0 && item.lws<8003)    //优惠券项
        var xinArr = dateArr.filter(item => item.lws == 8003 || item.lws == 8004)  //心愿单项
        dateArr.forEach((item,index)=>{
          quanArr.forEach(item1=>{
            if(item.id==item1.id){
              item1.idsId = index
            }
          })

          xinArr.forEach(item2 => {
            if (item.id == item2.id) {
              item2.xinId = index
            }
          })


        })
        console.log(quanArr,xinArr)
        this.setData({
          dateArr,
          todayDate,
          todayIdx,
          quanArr,
          xinArr
        })
      }
    })
  },

  //心愿单朋友圈
  getWishiList(){
    http.fetchApi('procedure_t', { p: 'R2015006' }).then(res=>{
      if(res.data.code==1){
        var wishListArr = res.data.results;
        this.setData({
          wishListArr
        })
      }
    })
  },

  //查询是否开启了签到提醒
  queryIsQian(){
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: 'R2016002|' + uid }).then(res=>{
      if(res.data.code==1){
        var qianTips = res.data.results[0].qds;
        var hasQian = res.data.results[0].bl_qiandao;
        qianTips == 0 ? qianTips = false : qianTips = true;
        this.setData({
          qianTips,
          hasQian
        })
      }
    })
  },

  //提醒签到
  switch1Change(e){
  
    var uid =app.globalData.uid;
    console.log(e.detail.value);
    this.setData({
      qianTips: e.detail.value
    })
    http.fetchApi('procedure_t', { p: 'R2016001|'+uid }).then(res=>{
      if(res.data.code==1){
  
        this.queryIsQian();
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   // this.getSignGift();
   
    this.queryIsQian();
    var day2 = new Date();
    day2.setTime(day2.getTime());
    var s2 = day2.getFullYear() + "-" + (day2.getMonth() + 1) + "-" + day2.getDate();
   
    this.querySign(s2);
    this.setData({
      today:s2,
      uid:app.globalData.uid,
    })
   
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getWishiList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})